package com.itfsw.jenkins.plugins;

import hudson.Extension;
import hudson.util.FormValidation;
import hudson.util.Secret;
import jenkins.model.GlobalConfiguration;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.ServletException;
import java.io.IOException;

/**
 * {Description}
 *
 * @author HW
 * @date 2021/12/10 18:22
 */
@Extension
public class QiniuConfiguration extends GlobalConfiguration {
    private String accessKey, secretKey;

    /**
     * 构造函数
     */
    public QiniuConfiguration() {
        load();
    }

    /**
     * 构造函数
     *
     * @param accessKey
     * @param secretKey
     */
    @DataBoundConstructor
    public QiniuConfiguration(String accessKey, String secretKey) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
    }

    /**
     * @return the Jenkins managed singleton for the configuration object
     */
    public static QiniuConfiguration get() {
        return GlobalConfiguration.all().get(QiniuConfiguration.class);
    }

    /**
     * Loads configuration values from frm data
     *
     * @param req      current request
     * @param formData form data
     * @return true when successfull
     * @throws FormException
     */
    @Override
    public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
        // To persist global configuration information,
        // set that to properties and call save().
        this.accessKey = formData.getString("accessKey");
        this.secretKey = Secret.fromString(formData.getString("secretKey")).getEncryptedValue();


        // ^Can also use req.bindJSON(this, formData);
        //  (easier when there are many fields; need set* methods for this, like setUseFrench)
        save();
        return true;
    }

    /**
     * Performs on-the-fly validation of the form field 'name'.
     *
     * @param value This parameter receives the value that the user has typed.
     * @return Indicates the outcome of the validation. This is sent to the
     * browser.
     * <p>
     * Note that returning {@link FormValidation#error(String)} does
     * not prevent the form from being saved. It just means that a
     * message will be displayed to the user.
     */
    public FormValidation doCheckAccessKey(@QueryParameter String value) throws IOException, ServletException {
        if (value.length() == 0)
            return FormValidation.error("Access Key 不能为空");
        return FormValidation.ok();
    }

    public FormValidation doCheckProfileName(@QueryParameter String value) throws IOException, ServletException {
        if (value.length() == 0)
            return FormValidation.error("设置项名称不能为空");
        return FormValidation.ok();
    }

    /**
     * Getter method for property <tt>accessKey</tt>.
     *
     * @return property value of accessKey
     */
    public String getAccessKey() {
        return accessKey;
    }

    /**
     * Getter method for property <tt>secretKey</tt>.
     *
     * @return property value of secretKey
     */
    public String getSecretKey() {
        return secretKey;
    }

    /**
     * Getter method for property <tt>secretKey</tt>.
     *
     * @return property value of secretKey
     */
    public String getSecretKeyPlainText() {
        return Secret.fromString(secretKey).getPlainText();
    }
}
